package com.asiainfo.lop;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.asiainfo.lop.net.VolleyInterface;
import com.asiainfo.lop.net.VolleyRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

/**
 * Created by wucm(wucm@asiainfo.com) on 2016/6/2 19:18.
 */
@EActivity(R.layout.crowd_detail_list)
public class CrowdDetailListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @ViewById(R.id.top_bar_tv)
    TextView topbarTv;

    @Extra("type")
    String type;

    @ViewById(R.id.crowd_detail_list)
    ListView crowdDetailListLv;

    @Click({R.id.btn_back_iv, R.id.btn_back_tv})
    void back() {
        this.finish();
    }

    @AfterViews
    void setTopBarTv() {
        String name = "";
        switch (type) {
            case "shedu": {
                name = "涉毒人群";
                break;
            }
            case "zhaoshi": {
                name = "肇事肇祸精神病人";
                break;
            }
            case "shekong": {
                name = "涉恐人群";
                break;
            }
            case "shangfang": {
                name = "重点上访人群";
                break;
            }
            case "xingshi": {
                name = "重大刑事前科人群";
                break;
            }
        }
        topbarTv.setText(name);
    }


    @ViewById(R.id.reflash_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    @AfterViews
    void init() {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        refreshData();
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    void refreshData() {
        String url = getString(R.string.rest_url_rooturl) + getString(R.string.rest_url_crowd_grouplist);

        url = url.replace("{groupTypeId}", "1");

        VolleyInterface volleyInterface = new VolleyInterface(this) {
            @Override
            public void success(JSONObject result) {
                swipeRefreshLayout.setRefreshing(false);

                flag = result.getString("flag");
                reason = result.getString("reason");

                if (!RestResultInfo.RESULT_SUCCESS_FLAG.equals(flag)) {
                    Toast.makeText(CrowdDetailListActivity.this, reason, Toast.LENGTH_LONG).show();
                    return;
                }

                loadData(result.getJSONArray("data"));
            }

            @Override
            public void error(VolleyError volleyError) {
                swipeRefreshLayout.setRefreshing(false);
            }
        };
        VolleyRequest.get(this, url, "CrowdDetailListActivity", volleyInterface);
    }

    /**
     * 加载数据
     *
     * @param data
     */
    void loadData(JSONArray data) {
        CrowdDetailListAdapt adapt = new CrowdDetailListAdapt(this, data);
        crowdDetailListLv.setAdapter(adapt);
    }

    @ItemClick(R.id.crowd_detail_list)
    public void listItemClicked(int position) {
        Intent intent = new Intent(this, CrowdDetailActivity_.class);
        startActivity(intent);
    }
}
